package listdemo;
//写一个泛型类，求泛型数组的最大值
//     将来传入的T一定要实现Comparable接口
class Alg<T extends Comparable<T>> {
    public T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }

}
class Student implements Comparable<Student>{

    @Override
    public int compareTo(Student o) {
        return 0;
    }
}
//写一个泛型方法，求泛型数组的最大值
class Alg2 {
    //泛型方法
    public<T extends Comparable<T>> T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}
public class Test2 {

    public static void main1(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Integer[] array = {1,2,3,4,6,10};
        Integer max = alg.findMax(array);
        System.out.println(max);
    }
    public static void main2(String[] args) {
        Student[] student = new Student[3];
        Alg<Student> alg2 = new Alg<Student>();
        Student max2 = alg2.findMax(student);
        System.out.println(max2);
    }
}
